/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "stdafx.h"
#include "S_PhysicsPBD.h"

#include "PBD解算器.h"
#include "PBF几何体.h"
#include "PBD物理体.h"

#include <ppl.h>
#include <三角形.h>
#include <SIMD/向量运算.h>

#include "数学/数学.h"


//#include "GPU解算/PBF_Simulation.h"
//template<typename T> _CPU_AND_GPU_CODE_ inline int hashIndex(const InfiniTAM::Vector3<T> vVoxPos, const int hashMask) {
//	return ((uint)(((uint)vVoxPos.x * 73856093) ^ ((uint)vVoxPos.y * 19349669) ^ ((uint)vVoxPos.z * 83492791)) & (uint)hashMask);
//}



S_Physics* f_PhysPBF_创建物理引擎() {
	S_PBF解算器* pbf = new S_PBF解算器();

	S_Tranform t;
	f_TranformInit(t);
	//auto* 边界 = f_PhysPBF_创建物理边界(pbf, {10, 10, 10}, t);
	//pbf->f_添加物理体(边界, 0);

	return pbf;
}

void f_PhysPBF_销毁物理引擎(S_Physics* phy) {
	delete phy;
}









S_物理体*	f_PhysPBF_创建物理流体(S_Physics* phy, S_物理材质& 材质, const S_Tranform& t, uint32 数量) {
	S_PBF解算器* pbf	= dynamic_cast<S_PBF解算器*>(phy);
	//_S_PBF流体* fluid	= new _S_PBF流体(材质, t, 数量);
	S_物理体* body = new S_物理体(nullptr);

	body->m_物理体类型 = E_物理体类型::E_物理体_流体;
	//body->m_几何体 = f_PBF创建流体body();
	//pbf->m_动态Body.push_back(fluid);
	//pbf->f_添加物理体(fluid, 0);
	pbf->m_流体.insert(body);

	return body;
}

S_物理体* f_PhysPBF_创建物理边界(S_Physics* phy, vec3 size, const S_Tranform& t) {
	S_PBF解算器* pbf = dynamic_cast<S_PBF解算器*>(phy);
	//S_PBF立方体几何体* box = new S_PBF立方体几何体(size, 10000);
	S_PBF边界* b = new S_PBF边界(size, pbf->m_粒子数据.m_粒子半径 * 2, t);
	return b;
}

S_物理体* f_PhysPBF_创建刚体(S_物理材质* 材质) {
	//S_PBF解算器* pbf = dynamic_cast<S_PBF解算器*>(phy);

	S_物理体* body = new S_物理体(nullptr);

	body->m_物理体类型 = E_物理体类型::E_物理体_刚体;
	//body->m_几何体 = f_PBF创建刚体body();

	return body;
}

S_物理体* f_PhysPBF_创建粒子发射(uint32 数量, S_物理材质* 材质) {
	S_物理体* body = new S_物理体(nullptr);

	S_PBF粒子发射* Data = f_PBF创建粒子发射();
	Data->m_数量 = 数量;

	body->m_物理体类型 = E_物理体类型::E_物理体_流入;
	
	return body;
} 


void f_PBF_set粒子半径(S_Physics* phy, float32 r) {
	S_PBF解算器* pbf = dynamic_cast<S_PBF解算器*>(phy);
	//pbf->m_邻接.m_几何体.m_Dim = 
	//pbf->m_粒子数据.m_粒子光滑半径 = pbf->m_邻接.m_几何体.m_域属性.m_单元属性.m_单元大小 * 1.5;
	//pbf->m_粒子数据.m_粒子光滑半径 = pbf->m_粒子数据.m_粒子半径 * 1.3;
	pbf->m_粒子数据.m_粒子光滑半径 = pbf->m_粒子数据.m_粒子半径 * 2.0;
	//pbf->m_粒子数据.m_粒子光滑半径 = pbf->m_粒子数据.m_粒子半径;
	float32 h3 = pbf->m_粒子数据.m_粒子光滑半径 * pbf->m_粒子数据.m_粒子光滑半径 * pbf->m_粒子数据.m_粒子光滑半径;
	//float32 h3 = pbf->m_邻接.m_几何体.m_域属性.m_单元属性.m_单元大小 * pbf->m_邻接.m_几何体.m_域属性.m_单元属性.m_单元大小 * pbf->m_邻接.m_几何体.m_域属性.m_单元属性.m_单元大小;
	//float32 h3 = pbf->m_邻接.m_几何体.m_域属性.m_单元属性.m_单元大小 * pbf->m_邻接.m_几何体.m_域属性.m_单元属性.m_单元大小 * pbf->m_邻接.m_几何体.m_域属性.m_单元属性.m_单元大小;
	//float32 h3 = r * r * r;
	pbf->m_粒子数据.m_GPU数据.m_K = 8.0 / (M_PI * h3);
	pbf->m_粒子数据.m_GPU数据.m_L = 48.0 / (M_PI * h3);
	pbf->m_粒子数据.m_GPU数据.m_Weight0 = pbf->f_权重({0,0,0});
	//std::cout << "pbf->m_权重0 = " << pbf->m_权重0 << std::endl;
	pbf->m_粒子数据.m_粒子半径 = r;
	
}

void f_PBF_set域(S_Physics* phy, const vec3& size, const vec3& offset, float32 r) {
	S_PBF解算器* pbf = dynamic_cast<S_PBF解算器*>(phy);

	

	pbf->m_粒子数据.m_粒子半径 = r * 2;
	//pbf->m_粒子数据.m_粒子半径 = r;

	//pbf->m_邻接.m_几何体.m_域属性.m_单元属性.m_域世界偏移 = offset;
	//pbf->m_邻接.m_几何体.m_域属性.m_单元属性.m_单元大小 = pbf->m_粒子数据.m_粒子半径 * 3;
	//pbf->m_邻接.m_几何体.m_域属性.m_单元属性.m_单元大小 = r * 0.5;
	//pbf->m_邻接.m_几何体.m_域属性.m_单元属性.m_单元大小 = r*2;

	uvec3 分辨率;
	//分辨率.x = size.x / pbf->m_邻接.m_几何体.m_域属性.m_单元属性.m_单元大小;
	//分辨率.y = size.y / pbf->m_邻接.m_几何体.m_域属性.m_单元属性.m_单元大小;
	//分辨率.z = size.z / pbf->m_邻接.m_几何体.m_域属性.m_单元属性.m_单元大小;
	//pbf->m_邻接.m_几何体.m_域属性.m_单元属性.m_分辨率 = 分辨率;
	//pbf->m_邻接.m_几何体.m_域属性.m_域世界偏移 = size * 0.5;
	//pbf->m_邻接.m_几何体.m_域属性.m_单元属性.m_世界大小 = size;
	

	f_空间划分_set叉树空间大小(pbf->m_粒子数据.m_静态网格空间划分, size.x, offset);
	f_PBF_set粒子半径(phy, r);
	
}

bool f_PhysPBF_切换物理体类型(S_物理体* body, E_物理体类型 类型) {

	switch (body->m_物理体类型) {
	case E_物理体类型::E_物理体_流体: f_PBF销毁流体body((S_PBF流体*)(body->m_几何体)); break;
	case E_物理体类型::E_物理体_刚体: f_PBF销毁刚体body((S_PBF刚体*)(body->m_几何体)); break;
	case E_物理体类型::E_物理体_流入: f_PBF销毁粒子发射((S_PBF粒子发射*)(body->m_几何体)); break;
	}

	switch (类型) {
	case E_物理体类型::E_物理体_流体: {
		//body->m_几何体 = f_PBF创建流体body();
		break;
	}

	case E_物理体类型::E_物理体_刚体: {
		//body->m_几何体 = f_PBF创建刚体body();
		break;
	}
		
	case E_物理体类型::E_物理体_流入: {
		//body->m_几何体 = f_PBF创建粒子发射();
		break;
	}
	default:
		break;
	}

	body->m_物理体类型 = 类型;

	return true;
}

bool f_PBF_添加物体(S_Physics* phy, S_物理体* body) {
	S_PBF解算器* pbf = dynamic_cast<S_PBF解算器*>(phy);

	//pbf->m_需要重构的物体.push_back(body);
	//pbf->m_需要重构物体 = true;
	switch (body->m_物理体类型)
	{
	case E_物理体类型::E_物理体_流体: {
		if (pbf->m_流体.find(body) == pbf->m_流体.end()) {
			pbf->m_流体.insert(body);
			pbf->f_添加物理体(body, 0);
		}
		break;
	}

	case E_物理体类型::E_物理体_刚体: {
		if (pbf->m_刚体.find(body) == pbf->m_刚体.end()) {
			pbf->m_刚体.insert(body);
			pbf->f_添加物理体(body, 0);
		}
		break;
	}

	case E_物理体类型::E_物理体_流入: {
		if (pbf->m_流入.find(body) == pbf->m_流入.end()) {
			pbf->m_流入.insert(body);
			pbf->f_添加物理体(body, 0);
		}
		break;
	}
	default:
		return false;
		break;
	}

	
	pbf->m_是否重构 = true;
	return true;
	
}

bool f_PBF_物体是否存在(S_Physics* phy, S_物理体* body) {
	S_PBF解算器* pbf = dynamic_cast<S_PBF解算器*>(phy);
	if (pbf->m_流体.find(body) != pbf->m_流体.end()) return true;
	return false;
}

void f_PhysPBF_add静态碰撞网格(S_Physics* phy, std::vector<vec3>& vert, std::vector<uvec3>& index) {
	S_PBF解算器* pbf = dynamic_cast<S_PBF解算器*>(phy);

	uint32 索引偏移起点 = pbf->m_粒子数据.m_静态三角网格顶点.size();
	pbf->m_粒子数据.m_静态三角网格顶点.insert(pbf->m_粒子数据.m_静态三角网格顶点.end(), vert.begin(), vert.end());

	uint32 num = index.size();
	uvec3* 源 = index.data();

	pbf->m_粒子数据.m_静态三角网格索引.resize(pbf->m_粒子数据.m_静态三角网格索引.size() + num);
	uvec3* 目标 = pbf->m_粒子数据.m_静态三角网格索引.data();
	for (uint32 i = 0; i < num; ++i) {
		目标[i] = 源[i] + 索引偏移起点;
	}

}




void f_PhysPBF_设置流体粒子数量(S_Physics* phy, S_物理体* body, uint32 num) {
	_S_PBF流体* fluid = dynamic_cast<_S_PBF流体*>(body);
	S_PBF解算器* pbf = dynamic_cast<S_PBF解算器*>(phy);

	fluid->m_Offset	= 0;
	fluid->m_Count	= num;

	pbf->m_粒子数据.m_位置.resize(num);
	pbf->m_粒子数据.m_速度.resize(num);
	pbf->m_粒子数据.m_质量.resize(num);

	pbf->m_粒子数据.m_上一帧位置.resize(num);
	pbf->m_粒子数据.m_位置量变化.resize(num);
	pbf->m_粒子数据.m_密度.resize(num);

	pbf->m_粒子数据.m_流体粒子总数 = num;

	//pbf->m_粒子数据.m_方向.resize(num);
	//pbf->m_粒子数据.m_计算后位置.resize(num);
	//pbf->m_粒子数据.m_动态邻接数量.resize(num);
	//pbf->m_粒子数据.m_动态邻接粒子索引.resize(num);
}

void f_PhysPBF_set粒子发射数量(S_物理体* body, uint32 数量) {
	switch (body->m_物理体类型)
	{
	case E_物理体类型::E_物理体_流体:
		break;

	case E_物理体类型::E_物理体_流入:
		((S_PBF粒子发射*)(body->m_几何体))->m_数量 = 数量;
		((S_PBF粒子发射*)(body->m_几何体))->m_以发射完 = false;
		break;
	default:
		break;
	}
}

void f_PhysPBF_set飞溅粒子数量(S_Physics* phy, uint32 数量)
{
	dynamic_cast<S_PBF解算器*>(phy)->m_粒子数据.m_飞溅粒子数量 = 数量;
	dynamic_cast<S_PBF解算器*>(phy)->m_是否重构 = true;
}

void f_PBF_set流体粒子区间(S_物理体* body, const uvec2& 区间)
{
	switch (body->m_物理体类型)
	{
	case E_物理体类型::E_物理体_流体:
		((S_PBF流体*)(body->m_几何体))->m_Offset = 区间.x;
		((S_PBF流体*)(body->m_几何体))->m_Count = 区间.y;
		break;

	case E_物理体类型::E_物理体_流入:
		((S_PBF粒子发射*)(body->m_几何体))->m_Offset = 区间.x;
		((S_PBF粒子发射*)(body->m_几何体))->m_Count = 区间.y;
		break;
	default:
		break;
	}
}

void f_PBF_重置发射体(S_物理体* body)
{
	if (body->m_物理体类型 == E_物理体_流入) {
		auto& e = *((S_PBF粒子发射*)(body->m_几何体));
		e.m_以发射完 = false;
		e.m_偏移 = 0;
	}
}

void f_PhysPBF_取粒子坐标(S_Physics* phy, S_物理体* body, vec3* c) {
	S_PBF解算器* pbf = dynamic_cast<S_PBF解算器*>(phy);
	_S_PBF流体* b = dynamic_cast<_S_PBF流体*>(body);

	vec3* s = pbf->m_粒子数据.m_位置.data();
	for (uint32 i = 0; i < b->m_Count; ++i) {
		c[i] = s[i + b->m_Offset];
	}
}

void f_PhysPBF_取粒子速度(S_Physics* phy, S_物理体* body, vec3* c) {
	S_PBF解算器* pbf = dynamic_cast<S_PBF解算器*>(phy);

	_S_PBF流体* b = dynamic_cast<_S_PBF流体*>(body);

	vec3* s = pbf->m_粒子数据.m_速度.data();

	for (uint32 i = 0; i < b->m_Count; ++i) {
		c[i] = s[i + b->m_Offset];
	}
}

S_体素* f_PhysPBF_get物体体素(S_物理体* body) {
	switch (body->m_物理体类型)
	{
	case E_物理体类型::E_物理体_流体:
		return ((S_PBF流体*)body->m_几何体)->m_体素;
	case E_物理体类型::E_物理体_刚体:
		return ((S_PBF刚体*)body->m_几何体)->m_体素;
	case E_物理体类型::E_物理体_流入:
		return ((S_PBF粒子发射*)body->m_几何体)->m_体素;
	default:
		break;
	}
	
	return nullptr;
}

uint64 f_PBF_get粒子总数(S_Physics* phy) {
	S_PBF解算器* pbf = dynamic_cast<S_PBF解算器*>(phy);
	return pbf->m_粒子数据.f_size();
}

S_域区* f_PBF_get域属性(S_Physics* phy) {
	S_PBF解算器* pbf = dynamic_cast<S_PBF解算器*>(phy);
	return pbf->f_get域属性();
}

float32 f_PBF_get粒子大小(S_Physics* phy) {
	S_PBF解算器* pbf = dynamic_cast<S_PBF解算器*>(phy);
	return pbf->m_粒子数据.m_粒子半径;
}

void f_PBF_get域中粒子(S_Physics* phy, vec3 coord, std::vector<vec3>& particle) {
	S_PBF解算器* pbf = dynamic_cast<S_PBF解算器*>(phy);
	return;
	S_cuAdjacencyIndex a = pbf->f_getGPU数据();
	//auto dom = pbf->m_邻接.m_几何体.m_域属性.m_单元属性;

	//coord = vec_Max(coord, dom.m_域世界偏移);
	//coord = vec_Min(coord, dom.m_世界大小 * 0.5f);
	
	//f_CUDA_get坐标范围粒子(pbf->m_粒子数据.m_GPU数据, a, dom, coord, particle);
}

uvec2 f_PBF_get物理体粒子区间(S_物理体* body)
{
	//S_PBF物理体* pb = dynamic_cast<S_PBF物理体*>(body);
	switch (body->m_物理体类型)
	{
	case E_物理体_流体:
		return { ((S_PBF流体*)(body->m_几何体))->m_Offset, ((S_PBF流体*)(body->m_几何体))->m_Count };
	case E_物理体_流入:
		return { ((S_PBF粒子发射*)(body->m_几何体))->m_Offset, ((S_PBF粒子发射*)(body->m_几何体))->m_Count };

	default:
		break;
	}
	return {};
	//return { pb->m_Offset, pb->m_Count};
}

uvec4 f_PBF_get粒子发射区间(S_物理体* body, float32 速率)
{
	if (body->m_物理体类型 == E_物理体_流入) {
		auto& e = *((S_PBF粒子发射*)(body->m_几何体));
		uint32 Offset = e.m_Offset;
		uint32 Count = e.m_Count;

		uvec4 发射区间;
		uint32 要发射数量 = e.m_体素->m_坐标.size() * 速率;

		if (e.m_偏移 + 要发射数量 < Count) {
			发射区间.x = e.m_偏移;
			e.m_偏移 += 要发射数量;
			发射区间.y = e.m_偏移;

			发射区间.z = 0;
			发射区间.w = 0;

		}
		else {
			发射区间.x = e.m_偏移;
			发射区间.y = Count;

			发射区间.z = 0;
			发射区间.w = Count - 发射区间.x;

			e.m_偏移 = 发射区间.w;
			e.m_以发射完 = true;

			return 发射区间;
		}

		if (e.m_以发射完) {
			发射区间.z = 0xffffffff;
			发射区间.w = 0xffffffff;
		}

		return 发射区间;
	}
	return uvec4();
}

uint64 f_PBF_get发射总数(S_物理体* body)
{
	switch (body->m_物理体类型)
	{
	case E_物理体_流体:
		return ((S_PBF流体*)(body->m_几何体))->m_Count;
	case E_物理体_流入:
		return ((S_PBF粒子发射*)(body->m_几何体))->m_数量;
		//return { pb->m_Offset, pb->m_Count };

	default:
		break;
	}
	return uint64();
}










vec3* f_PhysPBF_映射流体粒子坐标(S_Physics* phy, S_物理体* body) {
	S_PBF解算器* pbf = dynamic_cast<S_PBF解算器*>(phy);
	_S_PBF流体* b = dynamic_cast<_S_PBF流体*>(body);
	return &pbf->m_粒子数据.m_位置[b->m_Offset];
}

vec3* f_PhysPBF_映射流体粒子速度(S_Physics* phy, S_物理体* body) {
	S_PBF解算器* pbf = dynamic_cast<S_PBF解算器*>(phy);
	_S_PBF流体* b = dynamic_cast<_S_PBF流体*>(body);
	return &pbf->m_粒子数据.m_速度[b->m_Offset];
}

vec3* f_PhysPBF_映射流体粒子外力(S_Physics* phy, S_物理体* body) {
	S_PBF解算器* pbf = dynamic_cast<S_PBF解算器*>(phy);
	_S_PBF流体* b = dynamic_cast<_S_PBF流体*>(body);
	//return &pbf->m_粒子数据.m_方向[b->m_Offset];
	return nullptr;
}






void f_PhysPBF_施加单方向外力(S_Physics* phy, S_物理体* body, const vec3& f) {
	S_PBF解算器* pbf = dynamic_cast<S_PBF解算器*>(phy);
	_S_PBF流体* b = dynamic_cast<_S_PBF流体*>(body);

	vec3* 位置 = pbf->m_粒子数据.m_位置.data();
	vec3* 速度 = pbf->m_粒子数据.m_速度.data();
	//vec3* 外力 = pbf->m_粒子数据.m_方向.data();
	float32* 重量 = pbf->m_粒子数据.m_质量.data();
	//for (uint32 i = 0; i < b->m_Count; ++i) {
		//c[i] = s[i + b->m_Offset];
	//}
	Concurrency::parallel_for<uint32>(b->m_Offset, b->m_Count, [&](uint64 i) {
		if (重量[i] >= 0) {
			//外力[i] = f;
		}
	});
}












void f_PBF_构建域流体(S_Physics* phy, S_物理体* body) {
	S_PBF解算器* pbf = dynamic_cast<S_PBF解算器*>(phy);


	switch (body->m_物理体类型)
	{
		case E_物理体类型::E_物理体_流体: {
			S_PBF流体* b = (S_PBF流体*)body->m_几何体;
			S_体素* v = b->m_体素;

			uint64 num = v->m_维度.x * v->m_维度.y * v->m_维度.z;
			

		} break;


		default:
			break;
	}

}

void f_PhysPBF_构建静态碰撞空间划分(S_Physics* phy, float32 最小单元大小) {
	S_PBF解算器* pbf = dynamic_cast<S_PBF解算器*>(phy);


	auto p_index = pbf->m_粒子数据.m_静态三角网格索引.data();
	auto p_vert = pbf->m_粒子数据.m_静态三角网格顶点.data();
	uint32 num = pbf->m_粒子数据.m_静态三角网格索引.size();

	f_空间划分_remove所有子集(pbf->m_粒子数据.m_静态网格空间划分);
	for (uint32 i = 0; i < num; ++i) {
		auto index = p_index[i];
		S_Sphere c = f_graph_取三角形球形包围(p_vert[index.x], p_vert[index.y], p_vert[index.z]);
		
		auto* 单元 = f_空间划分_add子集(pbf->m_粒子数据.m_静态网格空间划分, c.pos, DEF_Max(最小单元大小, c.radius));
		if (单元->m_数据 == nullptr) {
			单元->m_数据 = new std::vector<uint32>();
		}
		((std::vector<uint32>*)单元->m_数据)->push_back(i);
	}
}








void f_PBF_流体数据写入到顶点缓存(S_Physics* 解算器, vec4* data) {
	S_PBF解算器* pbf = dynamic_cast<S_PBF解算器*>(解算器);

	vec3* loc = pbf->m_粒子数据.m_位置.data();
	uint64 count = pbf->m_粒子数据.m_位置.size();

#pragma omp parallel for
	for (int64 i = 0; i < count; ++i) {
		data[i] = { loc[i].x, loc[i].y, loc[i].z, 6.0f };
		//std::cout << "loc[i].x, loc[i].y, loc[i].z = " << loc[i].x << " " << loc[i].y << " " << loc[i].z << std::endl;
	}
}

void f_PBF_体素数据写入到顶点缓存(S_Physics* 解算器, std::vector<vec4>& data) {
	S_PBF解算器* pbf = dynamic_cast<S_PBF解算器*>(解算器);

	if (pbf->m_邻接.m_cpuIndexsNum.size() <= 0) return;


	//uvec3 dim = pbf->m_邻接.m_几何体.m_域属性.m_单元属性.m_分辨率 - uvec3{1, 1, 1};
	//vec3 offset = pbf->m_邻接.m_几何体.m_域属性.m_单元属性.m_域世界偏移;
	//float32 单元大小 = pbf->m_邻接.m_几何体.m_域属性.m_单元属性.m_单元大小;
	//pbf->m_邻接.m_几何体;
	//std::cout << "外力施加开始 : " << dim.x << "  单元大小 = " << 单元大小 << std::endl;

	//for (uint32 z = 1; z < dim.z; ++z) {
	//	for (uint32 y = 1; y < dim.y; ++y) {
			//std::vector<vec4> 当前行数据(dim.x);
//#pragma omp parallel for
	//		for (int32 x = 1; x < dim.x; ++x) {
	//			if (pbf->m_邻接.f_单元是否有填充({ uint32(x), uint32(y), uint32(z) })) {
					//uint8 voxel_type[27] = {};
					int32 vt = 0;
					uint8 vn = 0;
					/*for (int8 oz = -1; oz < 2; ++oz) {
						for (int8 oy = -1; oy < 2; ++oy) {
							for (int8 ox = -1; ox < 2; ++ox) {
								uvec3 coord = { x, y, z };
								coord.x += ox;
								coord.y += oy;
								coord.z += oz;

								if (pbf->m_邻接.f_单元是否有填充(coord))
									vt |= (1 << vn);
								++vn;
							}
						}
					}*/

					//uvec3 coord = { x, y - 1, z };
					//if (pbf->m_邻接.f_单元是否有填充(coord) == false) vt |= (1 << 4);
					//coord = { uint32(x), y + 1, z };
					//if (pbf->m_邻接.f_单元是否有填充(coord) == false) vt |= (1 << 22);

					//coord = { uint32(x) - 1, y, z };
					//if (pbf->m_邻接.f_单元是否有填充(coord) == false) vt |= (1 << 12);
					//coord = { uint32(x) + 1, y, z };
					//if (pbf->m_邻接.f_单元是否有填充(coord) == false) vt |= (1 << 14);

					//coord = { uint32(x), y, z - 1 };
					//if (pbf->m_邻接.f_单元是否有填充(coord) == false) vt |= (1 << 10);
					//coord = { uint32(x), y, z + 1 };
					//if (pbf->m_邻接.f_单元是否有填充(coord) == false) vt |= (1 << 16);

					//if (vt && vt < 134217727) {
					//if (vt && vt != 4281360) {
					//	vec3 v = (offset + vec3{ float32(x), float32(y), float32(z) } * 单元大小) + 单元大小 * 0.5;
					//	data.push_back({ v.x, v.y, v.z, float32(vt) });
						//当前行数据[x] = { v.x, v.y, v.z, float32(vt) };
					//}

				//}
			//}

			/*for (uint32 x = 1; x < dim.x; ++x) {
				if (当前行数据[x].w > 0) {
					data.push_back(当前行数据[x]);
				}
				//data.push_back({ v.x, v.y, v.z, float32(vt) });
			}*/
		//}
	//}

}

void f_PBF_静态碰撞空间树写入缓存(const S_Physics* 解算器, std::vector<vec4>& data) {
	const S_PBF解算器* pbf = dynamic_cast<const S_PBF解算器*>(解算器);
	f_空间划分_解析为坐标(pbf->m_粒子数据.m_静态网格空间划分, data);
}

void f_PBF_静态碰撞单元树写入缓存(const S_Physics* 解算器, std::vector<vec4>& data) {
	const S_PBF解算器* pbf = dynamic_cast<const S_PBF解算器*>(解算器);
	f_空间划分_单元解析为坐标(pbf->m_粒子数据.m_静态网格空间划分, data);
}







